﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using mal.WP7.GoogleAnalytics.Business.Repository;
using mal.WP7.GoogleAnalytics.Business.Resources;
using mal.WP7.GoogleAnalytics.ViewModels.Dashboard;
//using mal.WP7.GoogleAnalytics.Business.Resources;

namespace mal.WP7.GoogleAnalytics
{
    public class MainViewModel : ViewModelBase
    {
        #region Fields
        private ObservableCollection<DashboardItemBase> items;
        private DashboardItemBase selectedItem;
        private string selectedWebsiteId;
        private bool hasWebsites = false;
        private bool hasAccounts = true;
        private bool isLoading = false;
        #endregion

        #region Construction / Destruction
        public MainViewModel()
        {    
            Messenger.Default.Register<WebsitesAvailableMessage>(this, this.OnWebsitesAvailable);
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public ObservableCollection<DashboardItemBase> Items
        {
            get
            {
                return this.items;
            }

            set
            {
                if (this.items != value)
                {
                    this.items = value;
                    RaisePropertyChanged("Items");
                }
            }
        }

        public DashboardItemBase SelectedItem
        {
            get
            {
                return this.selectedItem;
            }

            set
            {
                if (this.selectedItem != value)
                {
                    this.selectedItem = value;
                    this.RaisePropertyChanged("SelectedItem");
                    this.ActiveSelectedItem();
                }
            }
        }

        public string SelectedWebsiteId
        {
            get
            {
                return this.selectedWebsiteId;
            }

            set
            {
                if (this.selectedWebsiteId != value)
                {
                    this.selectedWebsiteId = value;
                    RaisePropertyChanged("SelectedWebsiteId");
                }
            }
        }

        public bool HasWebsites
        {
            get
            {
                return this.hasWebsites;
            }

            set
            {
                if (this.hasWebsites != value)
                {
                    this.hasWebsites = value;
                    RaisePropertyChanged("HasWebsites");
                }
            }
        }

        public bool IsLoading
        {
            get
            {
                return this.isLoading;
            }

            set
            {
                if (this.isLoading != value)
                {
                    this.isLoading = value;
                    RaisePropertyChanged("IsLoading");
                }
            }
        }

        public bool HasAccounts
        {
            get
            {
                return this.hasAccounts;
            }

            set
            {
                if (this.hasAccounts != value)
                {
                    this.hasAccounts = value;
                    RaisePropertyChanged("HasAccounts");
                }
            }
        }
        #endregion

        #region Methods
        public override void Cleanup()
        {
            if (this.items != null)
            {
                foreach (DashboardItemBase item in this.items)
                {
                    item.Cleanup();
                }

                this.items.Clear();
                this.items = null;
            }
        }

        public void RegisterAccounts()
        {
            List<Website> websites = WebsiteRepository.Instance.Websites;

            if (WebsiteRepository.Instance.Accounts == null || WebsiteRepository.Instance.Accounts.Count == 0)
            {
                this.HasAccounts = false;
            }
            else
            {
                this.HasAccounts = true;
            }

            if (this.HasAccounts)
            {
                if (websites != null && websites.Count > 0)
                {
                    this.IsLoading = false;
                    this.HasWebsites = true;
                }
                else
                {
                    this.IsLoading = true;
                    this.HasWebsites = false;
                }
            }

            this.items = new ObservableCollection<DashboardItemBase>();
            this.items.Add(new DashboardItemDay());
            this.items.Add(new DashboardItemWeek());
            this.items.Add(new DashboardItemMonth());
            this.items.Add(new DashboardItemYear());
            this.RaisePropertyChanged("Items");

            this.SelectedItem = this.items[0];
        }

        public void ResetSelection()
        {
            if (this.Items != null)
            {
                foreach (DashboardItemBase item in this.Items)
                {
                    item.SelectedRow = null;
                }
            }
        }

        private void ActiveSelectedItem()
        {
            for (int i = 0; i < this.Items.Count; i++)
            {
                DashboardItemBase item = this.Items[i];

                if (item != this.selectedItem)
                {
                    item.IsActive = false;
                }
                else
                {
                    item.IsActive = true;
                    item.LoadDatas();

                    if (i < this.Items.Count - 1)
                    {
                        this.Items[i + 1].LoadDatas();
                    }
                }
            }
        }
        #endregion

        #region Events
        private void OnWebsitesAvailable(WebsitesAvailableMessage message)
        {
            if (message.Error != null)
            {
                this.IsLoading = false;

                MessageBox.Show(ResourceText.NetworkconnectionErrorMessage
                                    , ResourceText.NetworkconnectionErrorTitle
                                    , MessageBoxButton.OK);

            }
            else
            {
                this.IsLoading = false;
                List<Website> websites = WebsiteRepository.Instance.Websites;
                if (websites != null && websites.Count > 0)
                {
                    this.HasWebsites = true;
                    this.ActiveSelectedItem();
                }
                else
                {
                    this.HasWebsites = false;
                }
            }
        }
        #endregion
    }
}